package com.jiukuaidao.zhulin.module.commodity.presenter;

import android.widget.Toast;

import com.jiukuaidao.library.Config;
import com.jiukuaidao.library.net.MyRetroCallBack;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.zhulin.api.ApiGoodsDetail;
import com.jiukuaidao.zhulin.data.TasksRepository;
import com.jiukuaidao.zhulin.entity.ApiGoosDetailInfo;
import com.jiukuaidao.zhulin.entity.EventShoppingCartInfo;
import com.jiukuaidao.zhulin.module.BasePresenter;
import com.jiukuaidao.zhulin.module.commodity.contract.GoodsDetailContract;
import com.jiukuaidao.zhulin.util.AddressInfoUtil;

import java.util.List;

/**
 * Author : BinarySatan
 * Date : 2016/6/7
 * Description :
 */
public class GoodsDetailPresenter extends BasePresenter<GoodsDetailContract.View, TasksRepository> implements GoodsDetailContract.Presenter {

    public GoodsDetailPresenter(TasksRepository repository, GoodsDetailContract.View view) {
        super(repository, view);
    }

    @Override
    public void start() {
        loadGoodsDetailData();
    }

    @Override
    public void loadGoodsDetailData() {
        mView.showLoading(null);

        mRepository.loadData(new ApiGoodsDetail(mView.getGoodsId(), AddressInfoUtil.getAddressInfo().cityCode, new MyRetroCallBack<ApiGoosDetailInfo>() {
            @Override
            public void onSuccess(ApiGoosDetailInfo result, String errorCode, String msg) {
                if (result != null)
                    mView.getGoodsDetailDataSuc(result);

                mView.hideLoading();
            }

            @Override
            public void onFail(ApiGoosDetailInfo result, String errorCode, String msg) {
                if (Config.Debug)
                    ToastUtil.show("errorCode  :" + errorCode + "\n errorMsg :" + msg, Toast.LENGTH_SHORT);

                mView.hideLoading();
            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {
                if (Config.Debug)
                    ToastUtil.show("errorCode  :" + resCode + "\n errorMsg :" + msg, Toast.LENGTH_SHORT);

                mView.hideLoading();
            }
        }));

    }

    @Override
    public String findGoods() {
        List<EventShoppingCartInfo> datas = mView.getShoppingCartDataLocal();
        for (int i = 0; i < datas.size(); i++) {
            if (mView.getGoodsNumber().equals(datas.get(i).getGoodsGN()))
                return String.valueOf(datas.get(i).getCount());
        }
        return "0";
    }
}
